from flask import Blueprint, request
from common.libs.Helper import ops_renderJSON, iPagenation, ops_render
from common.models.record import Record
from common.models.book_detail import BookDetail
from application import db, app
from common.libs.DataHelper import getCurrentTime
import json, requests

record_page = Blueprint("record_page", __name__)


def pagessearch(seal, stime, etime):
    req = request.values
    stime = stime
    etime = etime
    page = 1
    if 'p' in req and req['p']:
        page = int(req['p'])

    page_params = {
        'total_count': seal.count(),
        "page_size": 12,
        'page': page,
        'url': "/record/recordsearch?starttime=" + stime + "&endtime=" + etime,
    }
    pages = iPagenation(page_params)
    # -1 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']
    seal = seal[offset:limit]

    return pages, seal


def pagesadmin(seal):
    req = request.values
    page = 1
    if 'p' in req and req['p']:
        page = int(req['p'])

    page_params = {
        'total_count': seal.count(),
        "page_size": 12,
        'page': page,
        'url': "/record/?",
    }
    pages = iPagenation(page_params)
    # -1 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']
    seal = seal[offset:limit]

    return pages, seal


@record_page.route('/', methods=['GET', 'POST'])
def recordadmin():
    record = Record.query.order_by(Record.time.desc()).limit(12)
    pages, record = pagesadmin(record)

    return ops_render("/admin/record.html", {"pages": pages, "record": record})


@record_page.route('/recordsearch', methods=['GET', 'POST'])
def recordsearch():
    starttime = request.args.get('starttime')
    endtime = request.args.get('endtime')

    record = Record.query.filter(
        Record.time.between(starttime, endtime)).order_by(
        Record.time.desc())

    pages, record = pagessearch(record, starttime, endtime)

    return ops_render("/admin/record.html", {"pages": pages, "record": record, "stime": starttime, "etime": endtime})


@record_page.route('/bookopenrecord', methods=['GET', 'POST'])
def bookopenrecord():
    req = request.values
    uid = req['uid'] if "uid" in req else ""
    part = req['part'] if "part" in req else ""
    action = req['action'] if "action" in req else ""
    classone = req['classone'] if "classone" in req else ""
    classtwo = req['classtwo'] if "classtwo" in req else ""
    project = req['project'] if "project" in req else ""
    filename = req['filename'] if "filename" in req else ""

    record = Record()
    record.part = part
    record.uid = uid
    record.classone = classone
    record.classtwo = classtwo
    record.project = project
    record.action = action
    record.filename = filename
    record.time = getCurrentTime()

    db.session.add(record)
    db.session.commit()

    return json.dumps({"code": 200, "msg": "记录成功"}, indent=4, sort_keys=True, default=str,
                      ensure_ascii=False)


@record_page.route('/parteropenrecord', methods=['GET', 'POST'])
def parteropenrecord():
    req = request.values
    uid = req['uid'] if "uid" in req else ""
    part = req['part'] if "part" in req else ""
    action = req['action'] if "action" in req else ""
    classone = req['classone'] if "classone" in req else ""
    classtwo = req['classtwo'] if "classtwo" in req else ""
    project = req['project'] if "project" in req else ""

    record = Record()
    record.part = part
    record.uid = uid
    record.classone = classone
    record.classtwo = classtwo
    record.project = project
    record.action = action
    record.filename = "资料文件"
    record.time = getCurrentTime()

    db.session.add(record)
    db.session.commit()

    return json.dumps({"code": 200, "msg": "记录成功"}, indent=4, sort_keys=True, default=str,
                      ensure_ascii=False)